
/**
 * @file main
 *
 */

/*********************
 *      INCLUDES
 *********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils/log.h"
#include <unistd.h>

#include "lv_drv_conf.h"
#include "lvgl/demos/lv_demos.h"
#include "lvgl/examples/lv_examples.h"
#include "lvgl/lvgl.h"
#include "style/style_btn.h"
#include "utils/msg.h"
#include "pages/card_table.h"
#if USE_SDL
#define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain"       \
                            issue*/
#include "pages/welcome.h"

#include "lv_drivers/sdl/sdl.h"
#elif USE_X11
#include "lv_drivers/x11/x11.h"
#endif
// #include "lv_drivers/display/monitor.h"
// #include "lv_drivers/indev/mouse.h"
// #include "lv_drivers/indev/keyboard.h"
// #include "lv_drivers/indev/mousewheel.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void hal_init(void);
static void hal_deinit(void);
static void *tick_thread(void *data);

/**********************
 *  STATIC VARIABLES
 **********************/
static pthread_t thr_tick;    /* thread */
static bool end_tick = false; /* flag to terminate thread */

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *      VARIABLES
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

int main(int argc, char **argv) {
	log_set_level(LOG_DEBUG);
    if (argc < 3) {
        log_error("Usage: %s <server_ip> <server_port>", argv[0]);
        log_error("Example: %s 127.0.0.1 8080", argv[0]);
        return 0;
    }

    /*Initialize LittlevGL*/
    lv_init();
    // msg_init();
    /*Initialize the HAL for LittlevGL*/
    hal_init();

    // 连接服务器
    int port = atoi(argv[2]);
    client_connect(argv[1],port);

    welcome_page_display();
    msg_init();

    while (1) {
        /* Periodically call the lv_task handler.
         * It could be done in a timer interrupt or an OS task too.*/
        pthread_mutex_lock(&mutex);
        lv_timer_handler();
        pthread_mutex_unlock(&mutex);
        usleep(5 * 1000);
    }

    hal_deinit();
    return 0;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/**
 * Initialize the Hardware Abstraction Layer (HAL) for the LVGL graphics
 * library
 */
static void hal_init(void) {
    /* mouse input device */
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1);
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /* keyboard input device */
    static lv_indev_drv_t indev_drv_2;
    lv_indev_drv_init(&indev_drv_2);
    indev_drv_2.type = LV_INDEV_TYPE_KEYPAD;

    /* mouse scroll wheel input device */
    static lv_indev_drv_t indev_drv_3;
    lv_indev_drv_init(&indev_drv_3);
    indev_drv_3.type = LV_INDEV_TYPE_ENCODER;

    lv_group_t *g = lv_group_create();
    lv_group_set_default(g);

    lv_disp_t *disp = NULL;

#if USE_SDL
    /* Use the 'monitor' driver which creates window on PC's monitor to simulate
     * a display*/
    sdl_init();

    /*Create a display buffer*/
    static lv_disp_draw_buf_t disp_buf1;
    static lv_color_t buf1_1[MONITOR_HOR_RES * 100];
    static lv_color_t buf1_2[MONITOR_HOR_RES * 100];
    lv_disp_draw_buf_init(&disp_buf1, buf1_1, buf1_2, MONITOR_HOR_RES * 100);

    /*Create a display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv); /*Basic initialization*/
    disp_drv.draw_buf = &disp_buf1;
    disp_drv.flush_cb = sdl_display_flush;
    disp_drv.hor_res = MONITOR_HOR_RES;
    disp_drv.ver_res = MONITOR_VER_RES;
    disp_drv.antialiasing = 1;

    disp = lv_disp_drv_register(&disp_drv);

    /* Add the input device driver */
    // mouse_init();
    indev_drv_1.read_cb = sdl_mouse_read;

    // keyboard_init();
    indev_drv_2.read_cb = sdl_keyboard_read;

    // mousewheel_init();
    indev_drv_3.read_cb = sdl_mousewheel_read;

#elif USE_X11
    lv_x11_init("LVGL Simulator Demo", DISP_HOR_RES, DISP_VER_RES);

    /*Create a display buffer*/
    static lv_disp_draw_buf_t disp_buf1;
    static lv_color_t buf1_1[DISP_HOR_RES * 100];
    static lv_color_t buf1_2[DISP_HOR_RES * 100];
    lv_disp_draw_buf_init(&disp_buf1, buf1_1, buf1_2, DISP_HOR_RES * 100);

    /*Create a display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf1;
    disp_drv.flush_cb = lv_x11_flush;
    disp_drv.hor_res = DISP_HOR_RES;
    disp_drv.ver_res = DISP_VER_RES;
    disp_drv.antialiasing = 1;

    disp = lv_disp_drv_register(&disp_drv);

    /* Add the input device driver */
    indev_drv_1.read_cb = lv_x11_get_pointer;
    indev_drv_2.read_cb = lv_x11_get_keyboard;
    indev_drv_3.read_cb = lv_x11_get_mousewheel;
#endif
    /* Set diplay theme */
    lv_theme_t *th = lv_theme_default_init(
        disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
        LV_THEME_DEFAULT_DARK, LV_FONT_DEFAULT);
    lv_disp_set_theme(disp, th);

    /* Tick init */
    end_tick = false;
    pthread_create(&thr_tick, NULL, tick_thread, NULL);

    /* register input devices */
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);
    lv_indev_t *kb_indev = lv_indev_drv_register(&indev_drv_2);
    lv_indev_t *enc_indev = lv_indev_drv_register(&indev_drv_3);
    lv_indev_set_group(kb_indev, g);
    lv_indev_set_group(enc_indev, g);

    /* Set a cursor for the mouse */
    LV_IMG_DECLARE(mouse_cursor_icon); /*Declare the image file.*/
    lv_obj_t *cursor_obj =
        lv_img_create(lv_scr_act()); /*Create an image object for the cursor*/
    lv_img_set_src(cursor_obj, &mouse_cursor_icon); /*Set the image source*/
    lv_indev_set_cursor(mouse_indev,
                        cursor_obj); /*Connect the image  object to the driver*/
}

/**
 * Releases the Hardware Abstraction Layer (HAL) for the LVGL graphics library
 */
static void hal_deinit(void) {
    end_tick = true;
    pthread_join(thr_tick, NULL);

#if USE_SDL
    // nop
#elif USE_X11
    lv_x11_deinit();
#endif
}

/**
 * A task to measure the elapsed time for LVGL
 * @param data unused
 * @return never return
 */
static void *tick_thread(void *data) {
    (void)data;

    while (!end_tick) {
        usleep(5000);
        lv_tick_inc(5); /*Tell LittelvGL that 5 milliseconds were elapsed*/
    }

    return NULL;
}
